package com.study.spring.security.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

//@Configuration
public class WebSecurityConfig1 extends WebSecurityConfigurerAdapter {
    @Bean
    public PasswordEncoder passwordEncoder() {
        //return NoOpPasswordEncoder.getInstance();
        return new BCryptPasswordEncoder();
    }

//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        String password = passwordEncoder().encode("123456");
//        auth
//                // 使用基于内存的 InMemoryUserDetailsManager
//                .inMemoryAuthentication()
//                //使用 PasswordEncoder 密码编码器
//                //.passwordEncoder(passwordEncoder())
//                // 配置用户
//                .withUser("user3").password(password).roles("admin")
//                // 配置其他用户
//                .and()
//                .withUser("user2").password(password).roles("user");
//    }

//    @Bean
//    @Override
//    // 与configure 冲突
//    protected UserDetailsService userDetailsService() {
//        return new UserDetailsService() {
//            @Override
//            public UserDetails loadUserByUsername(String username) throws
//                    UsernameNotFoundException {
//                String pw = passwordEncoder().encode("123456");
//                return new User("user", pw,
//                        AuthorityUtils.commaSeparatedStringToAuthorityList("admin,user"));
//            }
//        };
//    }
}
